<%@page import="javax.activation.MimetypesFileTypeMap"%>
<%@page import="javax.activation.FileTypeMap"%>
<%@page import="java.io.File"%>
<%@page import="java.io.FileInputStream"%>
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="java.util.Date"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=utf-8"
	pageEncoding="utf-8"%>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.9.1/themes/base/jquery-ui.css" />
<link href="${pageContext.request.contextPath}/css/org_manager.css"
	rel="stylesheet" type="text/css" />
<link href="${pageContext.request.contextPath}/css/add_new_org.css"
	rel="stylesheet" type="text/css" />
<script src="http://code.jquery.com/jquery-1.8.2.js"></script>
<script src="http://code.jquery.com/ui/1.9.1/jquery-ui.js"></script>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<jsp:useBean id="bill"
	class="team02.server.bean.BillModel" scope="page"></jsp:useBean>
<%
//権限をチェック
if(userBean.getAccessLevel()!=1){
	response.sendRedirect("404.html");
	return;
}
%>
<script>
$(function() {
	$('#datepicker').datepicker({
	changeMonth: true,
	changeYear: true,
	dateFormat: 'MM yy',
	onClose: function(dateText, inst) {
	var month = $("#ui-datepicker-div .ui-datepicker-month :selected").val();
	var year = $("#ui-datepicker-div .ui-datepicker-year :selected").val();
	$(this).datepicker('setDate', new Date(year, month, 1));
	},
	beforeShow: function(input, inst) {
	if ((datestr = $(this).val()).length > 0) {
	year = datestr.substring(datestr.length - 4, datestr.length);
	month = jQuery.inArray(datestr.substring(0, datestr.length - 5), $(this).datepicker('option', 'monthNames'));
	$(this).datepicker('option', 'defaultDate', new Date(year, month, 1));
	$(this).datepicker('setDate', new Date(year, month, 1));
	}
	}
	});
	});
</script>
<style>
.ui-datepicker-calendar {
display: none;
}
</style>
<c:choose>
<c:when test="${param.datepicker!=null }">
<c:set value='<%=bill.listAllOrganizationMoney(request.getParameter("datepicker"))%>' var="listOrgMoney"></c:set>
</c:when>
<c:otherwise>
<c:set value="<%=bill.listAllOrganizationMoney()%>" var="listOrgMoney"></c:set>
</c:otherwise>
</c:choose>
<%
if(request.getParameterMap().containsKey("export"))
{
	String filepath=application.getRealPath("")+ "\\bill\\";
	String filename=bill.writeCSV(request.getParameter("datepicker"), userBean.getUsername(), userBean.getName(),filepath);
	try{
	File file=new File(filepath+filename);
	FileInputStream fis = new FileInputStream(file);
	FileTypeMap ftm = new MimetypesFileTypeMap();
	response.setContentType(ftm.getContentType(filepath+filename));
	response.setHeader("Content-Disposition",
			"attachment; filename=" + file.getName());
	int iRead;
	while((iRead=fis.read())!=-1){
	response.getOutputStream().write(iRead);      
	}
	 response.getOutputStream().flush();
	}catch(Exception e){
	}finally{
		response.getOutputStream().close();
	}
}
%>
<div class="menu1"><%@include file="/jsputils/menu.jsp"%>
	<div class = "greeting_message">
		<%@include file="/jsputils/greeting.jsp"%>
	</div>
</div>
<div class="container">
<form action="" method="post" name="form">
	<div class="menu">
		<p class="menu_label">課金管理者</p>
		<p class="menu_item">
			<button type="submit" name="export" formaction="">データ出力</button>
			<button type="submit" formaction="bill_edit.t02"
				onclick="return check_selected()">単価編集</button>
		</p>
	</div>
	<div class="">
		<table style="width: 60%" class="content_table">
			<tr>
				<td>期間： </td>
				<td><input type="text" id="datepicker" name="datepicker" value="<%=request.getParameter("datepicker")!=null?request.getParameter("datepicker"): new SimpleDateFormat("MMMM yyyy").format(new Date())%>"/></td>
				<td><input type="submit" value="サブミット"></td>
			</tr>
		</table>
	</div>
		<table style="width: 100%" class="table_content">
			<tr>
				<th>団体名</th>
				<th>登録日</th>
				<th>期限日</th>
				<th>テスト数</th>
				<th>課金(VND)</th>

			</tr>
			<c:forEach var="orgItem" items="${listOrgMoney}">
				<tr>
					<td>${orgItem.name}</td>
					<td>${orgItem.getRegisteredDateString()}</td>
					<td>${orgItem.getExpiredDateString()}</td>
					<td>${orgItem.getNumberOfTest()}</td>
					<td>${orgItem.getPrice()}</td>
				</tr>
			</c:forEach>
		</table>
</form></div>